package cn.xingyun.pickaxe_loli_new.font;

import net.minecraft.ChatFormatting;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.font.FontSet;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;
import org.joml.Matrix4f;

import java.lang.reflect.Field;
import java.util.function.Function;

public class LoliFont extends Font {
    private static final Minecraft mc = Minecraft.getInstance();
    public LoliFont(Function<ResourceLocation, FontSet> p_243253_, boolean p_243245_) {
        super(p_243253_, p_243245_);
    }

    @NotNull
    public static Font getFont() throws ClassNotFoundException {
        Function<ResourceLocation, FontSet> fontt = null;
        Class fontc=Class.forName("net.minecraft.client.gui.Font");
        Field fontf = null;
        Field[] fields= fontc.getDeclaredFields();
        for (Field field :fields){
            if (field.getName().equals("fonts")){
                fontf=field;
                fontf.setAccessible(true);
            }else if (field.getName().equals("f_92713_")){
                fontf=field;
                fontf.setAccessible(true);
            }
        }
        if (fontf!=null) {
            try {
                fontt = (Function<ResourceLocation, FontSet>) fontf.get(mc.font);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            return new LoliFont(fontt, true);
        }else {
            return mc.font;
        }

    }
    public static double rangeRemap(double value, double low1, double high1, double low2, double high2)
    {
        return low2 + (value - low1) * (high2 - low2) / (high1 - low1);
    }
    public static long milliTime()
    {
        return System.nanoTime() / 1000000L;
    }
    @Override
    public int drawInBatch(@NotNull FormattedCharSequence formattedCharSequence, float x, float y, int rgb, boolean b1, @NotNull Matrix4f matrix4f, @NotNull MultiBufferSource multiBufferSource, @NotNull Font.DisplayMode mode, int i, int i1) {
        StringBuilder stringBuilder = new StringBuilder();
        formattedCharSequence.accept((index, style, codePoint) -> {
            stringBuilder.appendCodePoint(codePoint);
            return true;
        });
        String text = ChatFormatting.stripFormatting(stringBuilder.toString());

        float hue1 = (float) Util.getMillis() / 40000.0F % 1.0F;
        float hue2 = (float) Util.getMillis() / 35000.0F % 1.0F;
        float hueStep = (float)(0.025D + (Math.sin(((float)Util.getMillis() / 1200.0F)) % 6.28318D + 0.9D) * 0.1475D / 3.6D);
        float hue = (float) Util.getMillis() / 52000.0F % 1.0F;
        float huehuehue = (float)milliTime() / 500000.0F % 1.0F;
        float huehuehueStep = (float)rangeRemap(Math.sin((float)milliTime() / 1200.0F) % 6.28318D, -0.9D, 2.5D, 0.025D, 0.15D);
        float posX = x;
        if (text != null) {
            for (int index = 0; index < text.length(); index++) {
                String s = String.valueOf(text.charAt(index));

                float yOffset = (float) (Math.sin(i + (Util.getMillis() / 520f)) * 2);
                float yOffset1 = (float) (Math.sin(i + (Util.getMillis() / 420f)) * 2);
                float xOffset = (float) (Math.sin(i + (Util.getMillis() / 600f)) * 2);
                float xOffset1 = (float) (Math.sin(i + (milliTime() / 500f)+index));
                int c = 0xff000000 | Mth.hsvToRgb(hue1,0.5f,0.7f);
                int cd = 0x7f000000 | c;
                if (text.equals("LoliPickaxe")) {
                    super.drawInBatch(s, x - 0.5F, y - 0.5F + xOffset1, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                    super.drawInBatch(s, x, y + xOffset1, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                      /*super.drawInBatch(s, x-0.5F, y-0.5F, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                                    super.drawInBatch(s, x+0.5F, y-0.5F, csp, b1, matrix4f, multiBufferSource, mode, i, i1);
                                    super.drawInBatch(s, x-0.5F, y+0.5F, csp3, b1, matrix4f, multiBufferSource, mode, i, i1);
                                */
                }else if (text.contains("SSCG(3)")) {
                    if (s.equals("S") | s.equals("C") |s.equals("G") | s.equals("(") |s.equals("3")|s.equals(")")) {
                        super.drawInBatch(s, x - 0.5F, y - 0.5F, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                        super.drawInBatch(s, x, y, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                    }else {
                        super.drawInBatch(s, x, y,0xff000000 | 43520, b1, matrix4f, multiBufferSource, mode, i, i1);
                    }
                      /*super.drawInBatch(s, x-0.5F, y-0.5F, c, b1, matrix4f, multiBufferSource, mode, i, i1);
                                    super.drawInBatch(s, x+0.5F, y-0.5F, csp, b1, matrix4f, multiBufferSource, mode, i, i1);
                                    super.drawInBatch(s, x-0.5F, y+0.5F, csp3, b1, matrix4f, multiBufferSource, mode, i, i1);
                                */
                }else {
                    super.drawInBatch(s, x, y,11184810, b1, matrix4f, multiBufferSource, mode, i, i1);
                }

                huehuehue += huehuehueStep;
                huehuehue %= 1.0F;
                hue += hueStep;
                hue %= 1.0F;
                x += width(s);
            }
        }
        return (int) x;
    }

    public int drawInBatch (@NotNull String string,float x, float y, int rgb, boolean b,
                            @NotNull Matrix4f matrix4f, @NotNull MultiBufferSource source, @NotNull Font.DisplayMode mode, int i, int i1)
    {
        return drawInBatch(net.minecraft.network.chat.Component.literal(string).getVisualOrderText(), x, y, rgb, b, matrix4f, source, mode, i, i1);
    }

    public int drawInBatch (@NotNull net.minecraft.network.chat.Component component,float x, float y, int rgb,
                            boolean b, @NotNull Matrix4f matrix4f, @NotNull MultiBufferSource source, @NotNull Font.DisplayMode mode,
                            int i, int i1){
        return drawInBatch(component.getVisualOrderText(), x, y, rgb, b, matrix4f, source, mode, i, i1);
    }
}
